/** 
  Copyright (c) 2008, 2009 Claudio Imbrenda
  This program is released under the terms of the
  GNU GPL version 3 or later.
*/

package interfaccia;

import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.tools.*;
import macchina.*;

import static macchina.MRUtils.*;

/**
 *
 * @author  Claudio Imbrenda
 */
public class MRInstructionsPanel extends JPanel implements Logger{
  
    
  private static ResourceBundle bundle_interfaccia = 
          ResourceBundle.getBundle("interfaccia/Bundle_interfaccia");  
    
  /**
   * The instance of Macchina to work on.
   */
  private Macchina m;
  
  private volatile String curinstr;
  
  /**
   * Indicates whether the help panel is visible.
   */
  private boolean helpvisible = false;
  /**
   * 
   * Indicates whether the debug panel is visible.
   */
  private boolean debuggervisible = false;  
  
  /**
   * Indicates the help item being displayed. If null indicates that there is
   * no help item displayed.
   */
  private JLabel curhelp = null;
  
  private byte debug_AE;
  
  private boolean debug_AC;
  
  private int debug_TM;
  
  private volatile byte debug_uinstr=0;
  
  private volatile Thread runner;
  
  private Macchinina deb = new Macchinina(){
        private final static int _18 = 0x3FFFF;
        private final static int _10 = 0x3FF;
        private final static int _5 = 0x1F;

        private int A;
        private int B;
        private int N;
        private int[] mem = new int[1024];

        public int readTM() {
            return debug_TM&_18;
        }

        public int readTI() {
            return 0;
        }

        public int readA() {
            return A;
        }

        public int readB() {
            return B;
        }

        public int readN() {
            return N;
        }

        public void writeA(int v) {
            A=v&_18;
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                txtA.setText(niceformat(A,null,4));          
              }
            });
        }

        public void writeB(int v) {
            B=v&_18;
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                txtB.setText(niceformat(B,null,4));          
              }
            });            
        }

        public void writeN(short v) {
            N=v&_10;
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                txtN.setText(niceformat(N,null,4));          
              }
            });                                   
        }

        public void writeN(int v) {
            writeN((short)v);
        }

        public byte readAE() {
            return (byte)(debug_AE&_5);
        }

        public boolean readAC() {
            return debug_AC;
        }

        public void writeRU(byte b) {
            writeRU((int)b);
        }

        public void writeRU(int b) {
            log("\n   *** writeRU("+(b&_5)+");\n");
        }

        public int readMEM(short a) {
            return mem[a];
        }

        public void writeMEM(short a, final int v) {
            mem[a&_10]=v&_18;
            if(a==0){
              SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                  txtMEM0.setText(niceformat(v&_18,null,4));
                }
              });                                   
            }
        }

        public void setRunning(boolean b) {
            log("   *** setRunning("+b+");\n");
        }
      
  };
  
  

  public void log(String what) {
    txtaDebug.append(what+"\n");
  }
  
  
  /** Creates new form MRInstructionsPanel
   * @param mr the instance of Macchina to work on.
   */
  public MRInstructionsPanel(Macchina mr) {
    m=mr;
    initComponents();
    
    comboCurInstr.setModel(new ComboBoxModel() {
      String sel = null;
      List<ListDataListener> ld = new ArrayList<ListDataListener>();
      ActionListener al=null;
      
      public void setSelectedItem(Object anItem) {
        if(anItem instanceof String)sel=((String)anItem).toLowerCase();
      }

      public Object getSelectedItem() {
          try{
            if(sel==null)sel=(String)getElementAt(0);
          }catch(ClassCastException e){}
          return sel;
      }

      public int getSize() {
        return m.getInstructions().length;
      }

      public Object getElementAt(int index) {
        return m.getInstructions()[index];
      }

      public void addListDataListener(ListDataListener l) {
        ld.add(l);
        if(ld.size()==1){
          final ComboBoxModel me = this;
          m.addActionListener(al=new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              switch(e.getID()){
                case Macchina.INSTRUCTION_SET_CHANGED:
                  sel=null;
                  for(ListDataListener d:ld){
                    d.contentsChanged(
                            new ListDataEvent(me, 
                            ListDataEvent.CONTENTS_CHANGED, 0, getSize()));
                  }
                  break;
                default:
              }
            }
          });
        }
      }

      public void removeListDataListener(ListDataListener l) {
        ld.remove(l);
        if(ld.size()==0){
          m.removeActionListener(al);
          al=null;
        }
      }
    });
    
    m.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboCurInstrActionPerformed(e);
            }
        });
           
    jHelpPane.setVisible(false);
    helpvisible=false;
    
    jDebuggerPanel.setVisible(false);
    debuggervisible=false;
    jDocumentationPanel.setVisible(false);
    comboCurInstrActionPerformed(null);
  }
  
  /**
   * Displays a dialog announcing an error.
   * @param m the text of the error to be displayed.
   */
  private void showerror(String m){
    JOptionPane.showMessageDialog(this,
                  m, bundle_interfaccia.getString("ERRORE!"),
                  JOptionPane.ERROR_MESSAGE);
  }
  
  
  private void execinstr(final boolean single){
    if(debug_uinstr==0) debug_uinstr=1;
    
    if(single)
        step();
    else
        while(debug_uinstr!=0) step();
    
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            btnExecute.setEnabled(true);
            btnStep.setEnabled(true);
            btnStop.setEnabled(false);        
        }
    });

  }
  
  
  private void step(){
    runner = new Thread(new Runnable() {
        public void run() {
            _dostep();
        }
    });
    
    runner.start();    
    do
      try{
        runner.join();
      }catch(InterruptedException e){}
    while(runner.isAlive());
    
    runner=null;
  }
  
  
  private void _dostep(){
    if(debug_uinstr==0) debug_uinstr=1;
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           lblMicroinstr.setText(""+debug_uinstr);
        }
    });
    log("   *** "+bundle_interfaccia.getString("Microistruzione")+" \t"+debug_uinstr);
    
    Istruzione i=m.getInstruction(curinstr);
    if(i==null){
        log("   ******* "+bundle_interfaccia.getString("ERRORE")+"!! "+
                bundle_interfaccia.getString("Istruzione_\"")+curinstr+bundle_interfaccia.getString("\"_non_trovata!"));
        return;
    }
    
    Thread.yield();

    debug_uinstr=i.execute((short)0, deb, debug_uinstr, this);
  }
  
  
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        btnNewInstr = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        comboCurInstr = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        btnRemove = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaCode = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        btnEdit = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        btnDebugger = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        btnHelp = new javax.swing.JButton();
        jHelpPane = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        lblreadA = new javax.swing.JLabel();
        lblreadB = new javax.swing.JLabel();
        lblreadN = new javax.swing.JLabel();
        lblreadTM = new javax.swing.JLabel();
        lblreadTI = new javax.swing.JLabel();
        lblreadMEM = new javax.swing.JLabel();
        lblreadAC = new javax.swing.JLabel();
        lblreadAE = new javax.swing.JLabel();
        lblwriteA = new javax.swing.JLabel();
        lblwriteB = new javax.swing.JLabel();
        lblwriteN = new javax.swing.JLabel();
        lblwriteRU = new javax.swing.JLabel();
        lblwriteMEM = new javax.swing.JLabel();
        lblsetRunning = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        lblADDR = new javax.swing.JLabel();
        lbluinstr = new javax.swing.JLabel();
        lblMR = new javax.swing.JLabel();
        lbldebug = new javax.swing.JLabel();
        jDocumentationPanel = new javax.swing.JPanel();
        lblHelpTitle = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtaHelpText = new javax.swing.JTextArea();
        jDebuggerPanel = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        btnExecute = new javax.swing.JButton();
        btnStep = new javax.swing.JButton();
        jPanel18 = new javax.swing.JPanel();
        btnStop = new javax.swing.JButton();
        jPanel20 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        lblMicroinstrLabel = new javax.swing.JLabel();
        lblMicroinstr = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtA = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtB = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtN = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtTM = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txtAE = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtMEM0 = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        chkAC = new javax.swing.JCheckBox();
        jLabel20 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtaDebug = new javax.swing.JTextArea();
        jPanel19 = new javax.swing.JPanel();
        btnClear = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        btnNewInstr.setText(bundle_interfaccia.getString("Nuova...")); // NOI18N
        btnNewInstr.setToolTipText(bundle_interfaccia.getString("Aggiungi_nuova_istruzione...")); // NOI18N
        btnNewInstr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewInstrActionPerformed(evt);
            }
        });
        jPanel1.add(btnNewInstr);

        jLabel1.setText("   ");
        jPanel1.add(jLabel1);

        jLabel23.setLabelFor(comboCurInstr);
        jLabel23.setText(bundle_interfaccia.getString("Istruzione:_")); // NOI18N
        jPanel1.add(jLabel23);

        comboCurInstr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboCurInstr.setToolTipText(bundle_interfaccia.getString("Istruzione_corrente")); // NOI18N
        comboCurInstr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCurInstrActionPerformed(evt);
            }
        });
        jPanel1.add(comboCurInstr);

        jLabel2.setText("  ");
        jPanel1.add(jLabel2);

        btnRemove.setText(bundle_interfaccia.getString("Elimina")); // NOI18N
        btnRemove.setToolTipText(bundle_interfaccia.getString("Elimina_istruzione_corrente")); // NOI18N
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        jPanel1.add(btnRemove);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        txtaCode.setColumns(20);
        txtaCode.setRows(5);
        jScrollPane1.setViewportView(txtaCode);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        btnEdit.setText(bundle_interfaccia.getString("Modifica")); // NOI18N
        btnEdit.setToolTipText(bundle_interfaccia.getString("Modifica_istruzione_corrente")); // NOI18N
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jPanel2.add(btnEdit);

        jLabel3.setText("  ");
        jPanel2.add(jLabel3);

        btnSave.setText(bundle_interfaccia.getString("Salva")); // NOI18N
        btnSave.setToolTipText(bundle_interfaccia.getString("Salva_istruzione_modificata")); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel2.add(btnSave);

        jLabel4.setText("  ");
        jPanel2.add(jLabel4);

        btnCancel.setText(bundle_interfaccia.getString("Annulla")); // NOI18N
        btnCancel.setToolTipText(bundle_interfaccia.getString("Annulla_i_cambiamenti_effettuati")); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);

        jLabel6.setText("  ");
        jPanel2.add(jLabel6);

        btnDebugger.setText(bundle_interfaccia.getString("Mostra_Debugger")); // NOI18N
        btnDebugger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDebuggerActionPerformed(evt);
            }
        });
        jPanel2.add(btnDebugger);

        jLabel5.setText("  ");
        jPanel2.add(jLabel5);

        btnHelp.setText(bundle_interfaccia.getString("Mostra_Guida")); // NOI18N
        btnHelp.setToolTipText(bundle_interfaccia.getString("Mostra_o_nasconde_la_guida_rapida")); // NOI18N
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        jPanel2.add(btnHelp);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

        jHelpPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jHelpPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Class Macchinina"));
        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.Y_AXIS));

        lblreadA.setText("<html><i>int</i> <b>MR.readA( )");
        lblreadA.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadA.setMaximumSize(null);
        lblreadA.setMinimumSize(null);
        lblreadA.setPreferredSize(null);
        lblreadA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadA);

        lblreadB.setText("<html><i>int</i> <b>MR.readB( )");
        lblreadB.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadB.setMaximumSize(null);
        lblreadB.setMinimumSize(null);
        lblreadB.setPreferredSize(null);
        lblreadB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadB);

        lblreadN.setText("<html><i>int</i> <b>MR.readN( )");
        lblreadN.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadN.setMaximumSize(null);
        lblreadN.setMinimumSize(null);
        lblreadN.setPreferredSize(null);
        lblreadN.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadN);

        lblreadTM.setText("<html><i>int</i> <b>MR.readTM( )");
        lblreadTM.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadTM.setMaximumSize(null);
        lblreadTM.setMinimumSize(null);
        lblreadTM.setPreferredSize(null);
        lblreadTM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadTM);

        lblreadTI.setText("<html><i>int</i> <b>MR.readTI( )");
        lblreadTI.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadTI.setMaximumSize(null);
        lblreadTI.setMinimumSize(null);
        lblreadTI.setPreferredSize(null);
        lblreadTI.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadTI);

        lblreadMEM.setText("<html><i>int</i> <b>MR.readMEM(</b><i>short<b> a</b></i><b>)");
        lblreadMEM.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadMEM.setMaximumSize(null);
        lblreadMEM.setMinimumSize(null);
        lblreadMEM.setPreferredSize(null);
        lblreadMEM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadMEM);

        lblreadAC.setText("<html><i>boolean</i> <b>MR.readAC( )");
        lblreadAC.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadAC.setMaximumSize(null);
        lblreadAC.setMinimumSize(null);
        lblreadAC.setPreferredSize(null);
        lblreadAC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadAC);

        lblreadAE.setText("<html><i>byte</i> <b>MR.readAE( )");
        lblreadAE.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblreadAE.setMaximumSize(null);
        lblreadAE.setMinimumSize(null);
        lblreadAE.setPreferredSize(null);
        lblreadAE.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblreadAE);

        lblwriteA.setText("<html><i>void</i> <b>MR.writeA(</b><i>int <b>v</b></i><b>)");
        lblwriteA.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblwriteA.setMaximumSize(null);
        lblwriteA.setMinimumSize(null);
        lblwriteA.setPreferredSize(null);
        lblwriteA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblwriteA);

        lblwriteB.setText("<html><i>void</i> <b>MR.writeB(</b><i>int <b>v</b></i><b>)");
        lblwriteB.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblwriteB.setMaximumSize(null);
        lblwriteB.setMinimumSize(null);
        lblwriteB.setPreferredSize(null);
        lblwriteB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblwriteB);

        lblwriteN.setText("<html><i>void</i> <b>MR.writeN(</b><i>short <b>n</b></i><b>)");
        lblwriteN.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblwriteN.setMaximumSize(null);
        lblwriteN.setMinimumSize(null);
        lblwriteN.setPreferredSize(null);
        lblwriteN.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblwriteN);

        lblwriteRU.setText("<html><i>void</i> <b>MR.writeRU(</b><i>byte <b>b</b></i><b>)");
        lblwriteRU.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblwriteRU.setMaximumSize(null);
        lblwriteRU.setMinimumSize(null);
        lblwriteRU.setPreferredSize(null);
        lblwriteRU.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblwriteRU);

        lblwriteMEM.setText("<html><i>void</i> <b>MR.writeMEM(</b><i>short <b>a</b></i><b>, </b><i>int <b>v</b></i><b>)");
        lblwriteMEM.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblwriteMEM.setMaximumSize(null);
        lblwriteMEM.setMinimumSize(null);
        lblwriteMEM.setPreferredSize(null);
        lblwriteMEM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblwriteMEM);

        lblsetRunning.setText("<html><i>void</i> <b>MR.setRunning(</b><i>boolean <b>b</b></i><b>)");
        lblsetRunning.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblsetRunning.setMaximumSize(null);
        lblsetRunning.setMinimumSize(null);
        lblsetRunning.setPreferredSize(null);
        lblsetRunning.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel5.add(lblsetRunning);

        jPanel6.add(jPanel5, java.awt.BorderLayout.SOUTH);

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Instance Variables"));
        jPanel9.setLayout(new javax.swing.BoxLayout(jPanel9, javax.swing.BoxLayout.Y_AXIS));

        lblADDR.setText("<html><i>short</i> <b>ADDR</b>");
        lblADDR.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblADDR.setMaximumSize(null);
        lblADDR.setMinimumSize(null);
        lblADDR.setPreferredSize(null);
        lblADDR.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel9.add(lblADDR);

        lbluinstr.setText("<html><i>byte</i> <b>uinstr</b>");
        lbluinstr.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lbluinstr.setMaximumSize(null);
        lbluinstr.setMinimumSize(null);
        lbluinstr.setPreferredSize(null);
        lbluinstr.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel9.add(lbluinstr);

        lblMR.setText("<html><i>Macchinina</i> <b>MR");
        lblMR.setToolTipText(bundle_interfaccia.getString("Click_to_show_documentation")); // NOI18N
        lblMR.setMaximumSize(null);
        lblMR.setMinimumSize(null);
        lblMR.setPreferredSize(null);
        lblMR.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel9.add(lblMR);

        lbldebug.setText("<html><i>Logger</i> <b>debug");
        lbldebug.setMaximumSize(null);
        lbldebug.setMinimumSize(null);
        lbldebug.setPreferredSize(null);
        lbldebug.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblADDRMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblADDRMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblADDRMouseEntered(evt);
            }
        });
        jPanel9.add(lbldebug);

        jPanel6.add(jPanel9, java.awt.BorderLayout.NORTH);

        jPanel3.add(jPanel6, java.awt.BorderLayout.NORTH);

        jDocumentationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Documentation"));
        jDocumentationPanel.setLayout(new java.awt.BorderLayout());

        lblHelpTitle.setLabelFor(txtaHelpText);
        lblHelpTitle.setText(bundle_interfaccia.getString("Title")); // NOI18N
        lblHelpTitle.setToolTipText(bundle_interfaccia.getString("Current_help_item_displayed")); // NOI18N
        lblHelpTitle.setMaximumSize(null);
        lblHelpTitle.setMinimumSize(null);
        lblHelpTitle.setPreferredSize(null);
        jDocumentationPanel.add(lblHelpTitle, java.awt.BorderLayout.PAGE_START);

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        txtaHelpText.setColumns(20);
        txtaHelpText.setEditable(false);
        txtaHelpText.setLineWrap(true);
        txtaHelpText.setRows(5);
        txtaHelpText.setText("Qui ci verrà poi scritta la documentazione, fidatevi!");
        txtaHelpText.setToolTipText(bundle_interfaccia.getString("Documentation_for_the_selected_help_item")); // NOI18N
        txtaHelpText.setWrapStyleWord(true);
        txtaHelpText.setMaximumSize(null);
        txtaHelpText.setOpaque(false);
        jScrollPane3.setViewportView(txtaHelpText);

        jDocumentationPanel.add(jScrollPane3, java.awt.BorderLayout.LINE_START);

        jPanel3.add(jDocumentationPanel, java.awt.BorderLayout.CENTER);

        jHelpPane.setViewportView(jPanel3);

        add(jHelpPane, java.awt.BorderLayout.LINE_END);

        jDebuggerPanel.setLayout(new java.awt.BorderLayout());

        jPanel7.setLayout(new javax.swing.BoxLayout(jPanel7, javax.swing.BoxLayout.Y_AXIS));

        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.LINE_AXIS));

        btnExecute.setText(bundle_interfaccia.getString("Esegui")); // NOI18N
        btnExecute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExecuteActionPerformed(evt);
            }
        });
        jPanel8.add(btnExecute);

        btnStep.setText(bundle_interfaccia.getString("Passo")); // NOI18N
        btnStep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStepActionPerformed(evt);
            }
        });
        jPanel8.add(btnStep);

        jPanel7.add(jPanel8);

        jPanel18.setLayout(new javax.swing.BoxLayout(jPanel18, javax.swing.BoxLayout.LINE_AXIS));

        btnStop.setText(bundle_interfaccia.getString("Ferma!")); // NOI18N
        btnStop.setEnabled(false);
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });
        jPanel18.add(btnStop);

        jPanel7.add(jPanel18);

        jPanel20.setLayout(new java.awt.GridBagLayout());

        jPanel14.setLayout(new java.awt.BorderLayout());

        lblMicroinstrLabel.setLabelFor(lblMicroinstr);
        lblMicroinstrLabel.setText(bundle_interfaccia.getString("Microistruzione:_")); // NOI18N
        jPanel14.add(lblMicroinstrLabel, java.awt.BorderLayout.WEST);

        lblMicroinstr.setText("0");
        jPanel14.add(lblMicroinstr, java.awt.BorderLayout.EAST);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel20.add(jPanel14, gridBagConstraints);

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setLabelFor(txtA);
        jLabel9.setText("A");
        jLabel9.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel9, gridBagConstraints);

        txtA.setText("0x00000");
        txtA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAActionPerformed(evt);
            }
        });
        txtA.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtAFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 21;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtA, gridBagConstraints);

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setLabelFor(txtB);
        jLabel10.setText("B");
        jLabel10.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel10, gridBagConstraints);

        txtB.setText("0x00000");
        txtB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBActionPerformed(evt);
            }
        });
        txtB.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtBFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtB, gridBagConstraints);

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setLabelFor(txtN);
        jLabel11.setText("N");
        jLabel11.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel11, gridBagConstraints);

        txtN.setText("0x00000");
        txtN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNActionPerformed(evt);
            }
        });
        txtN.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtN, gridBagConstraints);

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setLabelFor(txtTM);
        jLabel12.setText("TM");
        jLabel12.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel12, gridBagConstraints);

        txtTM.setText("0x00000");
        txtTM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTMActionPerformed(evt);
            }
        });
        txtTM.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTMFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtTM, gridBagConstraints);

        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setLabelFor(txtAE);
        jLabel17.setText("AE");
        jLabel17.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel17, gridBagConstraints);

        txtAE.setText("0x00000");
        txtAE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAEActionPerformed(evt);
            }
        });
        txtAE.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtAEFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtAE, gridBagConstraints);

        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setLabelFor(txtMEM0);
        jLabel13.setText("MEM[0]");
        jLabel13.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel13, gridBagConstraints);

        txtMEM0.setText("0x00000");
        txtMEM0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMEM0ActionPerformed(evt);
            }
        });
        txtMEM0.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMEM0FocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(txtMEM0, gridBagConstraints);

        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel18.setLabelFor(chkAC);
        jLabel18.setText("AC");
        jLabel18.setPreferredSize(new java.awt.Dimension(48, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel20.add(jLabel18, gridBagConstraints);

        chkAC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkACActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel20.add(chkAC, gridBagConstraints);

        jLabel20.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        jPanel20.add(jLabel20, gridBagConstraints);

        jPanel7.add(jPanel20);

        jDebuggerPanel.add(jPanel7, java.awt.BorderLayout.NORTH);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setBorder(null);

        txtaDebug.setColumns(18);
        txtaDebug.setEditable(false);
        txtaDebug.setRows(5);
        jScrollPane2.setViewportView(txtaDebug);

        jPanel4.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel19.setLayout(new javax.swing.BoxLayout(jPanel19, javax.swing.BoxLayout.LINE_AXIS));

        btnClear.setText(bundle_interfaccia.getString("(svuota)")); // NOI18N
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        jPanel19.add(btnClear);

        jLabel19.setText(" ");
        jPanel19.add(jLabel19);

        jLabel7.setLabelFor(txtaDebug);
        jLabel7.setText(bundle_interfaccia.getString("Ouput_di_debug:")); // NOI18N
        jPanel19.add(jLabel7);

        jPanel4.add(jPanel19, java.awt.BorderLayout.PAGE_START);

        jDebuggerPanel.add(jPanel4, java.awt.BorderLayout.CENTER);

        add(jDebuggerPanel, java.awt.BorderLayout.LINE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewInstrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewInstrActionPerformed
    if(!MRUtils.hasJDK){
        showerror(bundle_interfaccia.getString("errore_nojdk"));
        btnNewInstr.setEnabled(false);
        return;
    }
    String res=JOptionPane.showInputDialog(
            bundle_interfaccia.getString("Inserire_il_nome_della_nuova_istruzione:"),"");
    if(res==null)return;
    if(!m.addInstruction(res, "return 0;",null))
      showerror(bundle_interfaccia.getString("Impossibile_aggiungere_l'istruzione_'")+res+bundle_interfaccia.getString("'."));
    else{
        res=res.toLowerCase();
        comboCurInstr.setSelectedItem(res);
        curinstr=res;
    }
    
}//GEN-LAST:event_btnNewInstrActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
    comboCurInstr.setEnabled(false);
    txtaCode.setEditable(true);
    btnEdit.setEnabled(false);
    btnSave.setEnabled(true);
    btnCancel.setEnabled(true);
    txtaCode.requestFocusInWindow();
}//GEN-LAST:event_btnEditActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    comboCurInstrActionPerformed(evt);
}//GEN-LAST:event_btnCancelActionPerformed

  private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    try{
      List<DiagnosticCollector<JavaFileObject>> l = 
              new ArrayList<DiagnosticCollector<JavaFileObject>>();
      String i = curinstr;
      if(m.alterInstruction(i, txtaCode.getText(), l))
        comboCurInstrActionPerformed(evt);
      else{
          List<Diagnostic<? extends JavaFileObject>> diags = 
                  l.get(0).getDiagnostics();
          
          log("   ****** "+bundle_interfaccia.getString("ERRORE_DI_COMPILAZIONE!")+" ******");          
          for(Diagnostic<? extends JavaFileObject> d:diags){
                log(d.toString());
          }
          log("   *******************************************");
          log("");
      }
      
    }catch(ClassCastException e){}
}//GEN-LAST:event_btnSaveActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    String delenda = curinstr;
    int idx = comboCurInstr.getSelectedIndex();
    comboCurInstr.setSelectedIndex((idx>0)?idx-1:idx+1);
        
    m.removeInstruction(delenda);
    
}//GEN-LAST:event_btnRemoveActionPerformed

  private void comboCurInstrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCurInstrActionPerformed
    try{
      String i = (String)comboCurInstr.getSelectedItem();
      if(i==null)return;
      curinstr = i;
      txtaCode.setText(m.getInstructionCode(i));
      txtaCode.setEditable(false);
      btnCancel.setEnabled(false);
      btnSave.setEnabled(false);
            
      btnEdit.setEnabled((i!=null)&&m.isAlterableInstr(i));
      
      comboCurInstr.setEnabled(true);
      
      btnRemove.setEnabled((i!=null)&&m.isRemovableInstr(i));
    }catch(ClassCastException e){}
}//GEN-LAST:event_comboCurInstrActionPerformed

  private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
    helpvisible=!helpvisible;
    jHelpPane.setVisible(helpvisible);
    btnHelp.setText(helpvisible?bundle_interfaccia.getString("Nascondi_Guida"):bundle_interfaccia.getString("Mostra_Guida"));
}//GEN-LAST:event_btnHelpActionPerformed

  private void lblADDRMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblADDRMouseEntered
      try{
        JLabel source = (JLabel)evt.getComponent();
        if(!source.getForeground().equals(Color.BLUE))
            source.setForeground(Color.RED);
      }catch(ClassCastException e){}
}//GEN-LAST:event_lblADDRMouseEntered

  private void lblADDRMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblADDRMouseExited
      try{
        JLabel source = (JLabel)evt.getComponent();
        if(source.getForeground().equals(Color.RED))
            source.setForeground(UIManager.getColor("Label.foreground"));
      }catch(ClassCastException e){}      
}//GEN-LAST:event_lblADDRMouseExited

  private void lblADDRMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblADDRMouseClicked
      try{
        JLabel source = (JLabel)evt.getComponent();
        if(curhelp==source){
            jDocumentationPanel.setVisible(false);
            curhelp=null;
            source.setForeground(UIManager.getColor("Label.foreground"));
        }else {
            if(curhelp!=null)
                curhelp.setForeground(UIManager.getColor("Label.foreground"));
            
            curhelp=source;
            source.setForeground(Color.BLUE);
            jDocumentationPanel.setVisible(true);
            lblHelpTitle.setText(source.getText());
            
        }
      }catch(ClassCastException e){}
}//GEN-LAST:event_lblADDRMouseClicked

  private void btnDebuggerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDebuggerActionPerformed
    if(!MRUtils.hasJDK){
        showerror(bundle_interfaccia.getString("errore_nojdk"));
        btnDebugger.setEnabled(false);
        return;
    }
    debuggervisible=!debuggervisible;
    if(debuggervisible)
      txtaDebug.setText("");    
    jDebuggerPanel.setVisible(debuggervisible);
    btnDebugger.setText(debuggervisible?
        bundle_interfaccia.getString("Nascondi_Debugger"):bundle_interfaccia.getString("Mostra_Debugger"));
}//GEN-LAST:event_btnDebuggerActionPerformed

  private void txtAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAActionPerformed
    deb.writeA(understand(txtA.getText(), m, deb.readA()));
    txtA.setText(niceformat(deb.readA(),null,4));    
}//GEN-LAST:event_txtAActionPerformed

  private void txtMEM0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMEM0ActionPerformed
    deb.writeMEM((short)0,
            understand(txtMEM0.getText(),m,deb.readMEM((short)0)));
    txtMEM0.setText(niceformat(deb.readMEM((short)0),null,4));
}//GEN-LAST:event_txtMEM0ActionPerformed

  private void chkACActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkACActionPerformed
    debug_AC=chkAC.isSelected();
}//GEN-LAST:event_chkACActionPerformed

  private void txtAEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAEActionPerformed
      debug_AE=(byte)understand(txtAE.getText(), m, debug_AE);
      txtAE.setText(niceformat(debug_AE,null,4));
}//GEN-LAST:event_txtAEActionPerformed

  private void txtBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBActionPerformed
    deb.writeB(MRUtils.understand(txtB.getText(), m, deb.readB()));
    txtB.setText(niceformat(deb.readB(),null,4));
}//GEN-LAST:event_txtBActionPerformed

  private void txtNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNActionPerformed
    deb.writeN(understand(txtN.getText(), m, deb.readN()));
    txtN.setText(niceformat(deb.readN(),null,4));
}//GEN-LAST:event_txtNActionPerformed

  private void txtTMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTMActionPerformed
      debug_TM=(byte)understand(txtTM.getText(), m, debug_TM);
      txtTM.setText(niceformat(debug_TM,null,4));
}//GEN-LAST:event_txtTMActionPerformed

  private void btnExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExecuteActionPerformed
    btnExecute.setEnabled(false);
    btnStep.setEnabled(false);
    btnStop.setEnabled(true);
    
    (new Thread(){
        public void run(){
            execinstr(false);        
        }
    }).start();
}//GEN-LAST:event_btnExecuteActionPerformed

  private void btnStepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStepActionPerformed
    btnExecute.setEnabled(false);
    btnStep.setEnabled(false);
    btnStop.setEnabled(true);
    
    (new Thread(){
        public void run(){
            execinstr(true);        
        }
    }).start();
}//GEN-LAST:event_btnStepActionPerformed

  private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
    try{
        debug_uinstr=0;
        runner.stop();
    }catch(NullPointerException ignore){}
}//GEN-LAST:event_btnStopActionPerformed

  private void txtAFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAFocusLost
    txtAActionPerformed(null);
}//GEN-LAST:event_txtAFocusLost

  private void txtBFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBFocusLost
    txtBActionPerformed(null);
}//GEN-LAST:event_txtBFocusLost

  private void txtNFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNFocusLost
    txtNActionPerformed(null);
}//GEN-LAST:event_txtNFocusLost

  private void txtTMFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTMFocusLost
    txtTMActionPerformed(null);
}//GEN-LAST:event_txtTMFocusLost

  private void txtAEFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAEFocusLost
    txtAEActionPerformed(null);
}//GEN-LAST:event_txtAEFocusLost

  private void txtMEM0FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMEM0FocusLost
    txtMEM0ActionPerformed(null);
}//GEN-LAST:event_txtMEM0FocusLost

  private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
    txtaDebug.setText("");
}//GEN-LAST:event_btnClearActionPerformed
  
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnDebugger;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnExecute;
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnNewInstr;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnStep;
    private javax.swing.JButton btnStop;
    private javax.swing.JCheckBox chkAC;
    private javax.swing.JComboBox comboCurInstr;
    private javax.swing.JPanel jDebuggerPanel;
    private javax.swing.JPanel jDocumentationPanel;
    private javax.swing.JScrollPane jHelpPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblADDR;
    private javax.swing.JLabel lblHelpTitle;
    private javax.swing.JLabel lblMR;
    private javax.swing.JLabel lblMicroinstr;
    private javax.swing.JLabel lblMicroinstrLabel;
    private javax.swing.JLabel lbldebug;
    private javax.swing.JLabel lblreadA;
    private javax.swing.JLabel lblreadAC;
    private javax.swing.JLabel lblreadAE;
    private javax.swing.JLabel lblreadB;
    private javax.swing.JLabel lblreadMEM;
    private javax.swing.JLabel lblreadN;
    private javax.swing.JLabel lblreadTI;
    private javax.swing.JLabel lblreadTM;
    private javax.swing.JLabel lblsetRunning;
    private javax.swing.JLabel lbluinstr;
    private javax.swing.JLabel lblwriteA;
    private javax.swing.JLabel lblwriteB;
    private javax.swing.JLabel lblwriteMEM;
    private javax.swing.JLabel lblwriteN;
    private javax.swing.JLabel lblwriteRU;
    private javax.swing.JTextField txtA;
    private javax.swing.JTextField txtAE;
    private javax.swing.JTextField txtB;
    private javax.swing.JTextField txtMEM0;
    private javax.swing.JTextField txtN;
    private javax.swing.JTextField txtTM;
    private javax.swing.JTextArea txtaCode;
    private javax.swing.JTextArea txtaDebug;
    private javax.swing.JTextArea txtaHelpText;
    // End of variables declaration//GEN-END:variables
  
}
